//
//  WeaponSearch.m
//  SpaceStorm
//
//  Created by 祥 雍 on 13-7-21.
//  Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
//

#import "WeaponFSM.h"
#import "Weapon.h"

#pragma mark -
#pragma mark WeaponSearch,搜敌状态机
@implementation WeaponSearch
@synthesize manager = _manager;

+(id) searchStateWithManager:(WeaponFSMManager*)manager
{
    return [[[self alloc] initStateWithManager:manager] autorelease]; 
}

-(id) initStateWithManager:(WeaponFSMManager*)manager
{
    self = [super init]; 
	
	if (self!= nil) 
    {
        self.manager =  manager;
    }
    
    return self;
}

-(void) enter
{
    CCLOG(@"武器：进入搜敌状态机");
    //TODO
}

-(void) execute
{
    //TODO
}

-(void) exit
{
    CCLOG(@"武器：退出搜敌状态机");
    //TODO
}

- (void) dealloc
{
    if(_manager != nil)
    {
        [_manager release];
        _manager = nil;
    }
    
    [super dealloc];
}

@end

#pragma mark -
#pragma mark WeaponLockTarget,锁敌状态机
@implementation WeaponLockTarget

@synthesize manager = _manager;

+(id) lockTargeStateWithManager:(WeaponFSMManager*)manager
{
    return [[[self alloc] initStateWithManager:manager] autorelease];
}

-(id) initStateWithManager:(WeaponFSMManager*)manager
{
    self = [super init]; 
	
	if (self!= nil) 
    {
        self.manager =  manager;
    }
    
    return self;
}

-(void) enter
{
    CCLOG(@"武器：进入锁定状态机");
    //TODO
}

-(void) execute
{
    //TODO
}

-(void) exit
{
    CCLOG(@"武器：退出锁定状态机");
    //TODO
}

- (void) dealloc
{
    if(_manager != nil)
    {
        [_manager release];
        _manager = nil;
    }
    
    [super dealloc];
}

@end

#pragma mark -
#pragma mark WeaponFire,开火状态机
@implementation WeaponFire

@synthesize manager = _manager;

+(id) fireStateWithManager:(WeaponFSMManager*)manager
{
    return [[[self alloc] initStateWithManager:manager] autorelease];
}

-(id) initStateWithManager:(WeaponFSMManager*)manager
{
    self = [super init]; 
	
	if (self!= nil) 
    {
        self.manager =  manager;
    }
    
    return self;
}

-(void) enter
{
    CCLOG(@"武器：进入开火状态机");
    //TODO
}

-(void) execute
{
    //TODO
}

-(void) exit
{
    CCLOG(@"武器：退出开火状态机");
    //TODO
}

- (void) dealloc
{
    if(_manager != nil)
    {
        [_manager release];
        _manager = nil;
    }
    
    [super dealloc];
}
@end

#pragma mark -
#pragma mark WeaponFire,武器的状态机管理器
@implementation WeaponFSMManager

@synthesize weapon = _weapon;
@synthesize searchState = _searchState;
@synthesize lockTargetState = _lockTargetState;
@synthesize fireState = _fireState;

+(id) managerWithWeapon:(Weapon*)weapon
{
    return [[[self alloc] initWithWeapon:weapon] autorelease];
}

-(id) initWithWeapon:(Weapon*)weapon
{
    self = [super init]; 
	
	if (self!= nil) 
    {
        //纪录武器的引用
        self.weapon             = weapon;
        //创建3个状态机
        self.searchState        = [WeaponSearch searchStateWithManager:self];
        self.lockTargetState    = [WeaponLockTarget lockTargeStateWithManager:self];
        self.fireState          = [WeaponFire fireStateWithManager:self];
        //默认状态为搜敌状态
        [self toSearch];
    }
    
    return self;
}

/**
	进入搜敌状态
 */
-(void) toSearch
{
    [super changeState:_searchState];
}

/**
	进入锁敌状态
 */
-(void) toLockTarget
{
     [super changeState:_lockTargetState];
}

/**
	进入开火状态
 */
-(void) toFire
{
    [super changeState:_fireState];
}

- (void) dealloc
{
    if(_weapon != nil)
    {
        [_weapon release];
        _weapon = nil;
    }
    
    if(_searchState != nil)
    {
        [_searchState release];
        _searchState = nil;
    }
    
    if(_lockTargetState != nil)
    {
        [_lockTargetState release];
        _lockTargetState = nil;
    }
    
    if(_fireState != nil)
    {
        [_fireState release];
        _fireState = nil;
    }
    
    [super dealloc];
}

@end